<?php

namespace app\admin\model;

use think\Model;
use think\facade\Session;
use app\admin\validate\Admin as validateClass;

class Admin extends Model
{
    protected $table = 'tp_admin';
    //登录验证
    public function login($data){
        $validate = new validateClass;
        if (!$validate->scene('login')->check($data)){
            return $validate->getError();
        }
        $ip = $data['ip'];
        unset($data['ip']);
        $result = $this->where($data)->find();

        if ($result){
            //判断用户登录状态
            if ($result['status'] != 1){
                return '账户状态异常!';
            }
            //将用户登录状态写入session
            $sessionData = [
                'username' => $result['username'],
                'nickname' => $result['nickname'],
                'email'    => $result['email'],
                'is_super' => $result['is_super'],
                'ip'       => $ip
            ];
            Session::set('admin',$sessionData);
            //写登录日志
            writeLog($data['username'],$data['password'],$ip);
            //登陆数据与数据库中的数据匹配，返回1代表登陆成功
            return 1;
        }else{
            return '用户名或密码错误';
        }
    }

    //注册验证
    public function register($data){
        $validate = new validateClass;
        if (!$validate->scene('register')->check($data)){
            return $validate->getError();
        }
        if($this->allowField(true)->save($data)){
            return 1;
        }else{
            return '注册失败';
        }
    }

    //忘记密码邮箱验证
    public function forget($data){
        $validate = new validateClass;
        if (!$validate->scene('forget')->check($data)){
            return $validate->getError();
        }
        $result = $this->where($data)->find();
        if ($result){
            return 1;
        }else{
            return '邮箱不存在';
        }
    }

    //重置随机密码
    public function reset($data){
        $validate = new validateClass;
        if (!$validate->scene('reset')->check($data)){
            return $validate->getError();
        }
        $result = $this->where('email','=',$data['email'])->find();
        if ($data['codein'] == $data['code']){
            $newPassword = mt_rand(10000,99999);
            $result['password'] = $newPassword;
            if ($result->save()){
                mailto($data['email'],'重置密码成功！','您的邮箱所绑定的账户'.$result['username'].'的新密码为'.$newPassword);
                return 1;
            }else{
                return '重置密码失败';
            }
        }else{
            return '验证码错误';
        }

    }

    //修改密码
    public function password($data){
        $validate = new validateClass;
        if (!$validate->scene('password')->check($data)){
            return $validate->getError();
        }
        $result = $this->where('username',Session::get('admin')['username'])->find();
        $result['password'] = $data['password'];
        if ($result->save()){
            return 1;
        }else{
            return '密码修改失败';
        }

    }
}
